package com.agy.filemanager.controller;

import com.agy.filemanager.common.PageUtil;
import com.agy.filemanager.entity.ProMainType;
import com.agy.filemanager.entity.ProType;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.service.ProMainTypeService;
import com.agy.filemanager.service.ProTypeService;
import com.agy.filemanager.toolClass.Result;
import com.agy.filemanager.toolClass.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PUT;
import java.util.HashMap;
import java.util.List;

/**
 *   项目大类控制层
 *  */
@RestController
@RequestMapping("pro/maintype")
public class ProMainTypeController {

    @Autowired
    private ProMainTypeService proMainTypeService;

    @Autowired
    private ProTypeService proTypeService;

    /**
     * 保存项目大分类
     * @param name
     * @return
     */
    @PostMapping("/entity")
    public Result insertProMainType(@RequestParam("name") String name){
        String id = proMainTypeService.insertProMainType(name);
        return ResultUtil.success(id);
    }

    /**
     * 根据id修改项目大分类
     * @param
     * @return
     */
    @PutMapping("/entity")
    public Result updateProMainTypeById(String id, String name){
        ProMainType entity = new ProMainType();
        entity.setId(id);
        entity.setName(name);
        proMainTypeService.updateProMainTypeById(entity);
        return  ResultUtil.success("修改成功");
    }

    /**
     * 根据id删除项目大类
     * @param id
     * @return
     */
    @DeleteMapping("/entity")
    public Result deleteProMainTypeById( String id){
        //判断大类下面有没有项目分类
        PageUtil<ProType> proTypes = proTypeService.getProTypeList(id,"");
        if(proTypes.getRows().size() > 0){
            return ResultUtil.error("不能删除含有项目分类的节点！");
        }
        proMainTypeService.deleteProMainTypeById(id);
        return  ResultUtil.success("删除成功");
    }

    /**
     * 获取项目大类的树形列表
     * @return
     */
    @GetMapping("tree")
    public ResponseEntity getProMainTypeTreeNode(){
        List<TreeModel> treeMap = proMainTypeService.getProMainTypeTreeNode();
        return ResponseEntity.ok(treeMap);
    }
}
